package com.tbit.uqbike.client.handler.borrowbikehandler;

import com.tbit.uqbike.client.common.ResultMsg;
import com.tbit.uqbike.client.dao.core.BorrowCheckConfigLogDao;
import com.tbit.uqbike.client.pojo.borrowbike.BorrowCheckConfig;
import com.tbit.uqbike.client.pojo.borrowbike.BorrowCheckConfigLog;
import com.tbit.uqbike.client.vo.borrow.BorrowBikeVO;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Objects;

/**
 * @author lh2
 * @ClassName: AbstractCheckHandler
 * @Description: TODO
 * @date 2023-07-27 07:30:23
 */
@Slf4j
public abstract class AbstractCheckHandler {
    /**
     * 处理器配置
     */
    @Setter
    @Getter
    protected BorrowCheckConfig config;
    /**
     * 当前处理器持有下一个处理器的引用
     */
    @Getter
    @Setter
    private AbstractCheckHandler nextHandler;
    @Autowired
    private BorrowCheckConfigLogDao borrowCheckConfigLogMapper;

    /**
     * 处理器执行方法
     */
    public abstract ResultMsg handle(BorrowBikeVO vo, String serialNO);

    /**
     * 链路传递
     */
    protected ResultMsg next(BorrowBikeVO vo, String serialNO) {
        // log.info("调用next方法，config={}，nextHandler={}", config, nextHandler);
        // 下一个链路没有处理器了，直接返回
        if (Objects.isNull(nextHandler)) {
            return ResultMsg.success();
        }
        // 执行下一个处理器
        BorrowCheckConfigLog bccLog = new BorrowCheckConfigLog();
        bccLog.setSerialNO(serialNO);
        bccLog.setAccountId(vo.getUser().getAccountId());
        bccLog.setUserId(vo.getUser().getUserId());
        bccLog.setHandlerName(nextHandler.getConfig().getHandlerName());
        bccLog.setOrderNum(nextHandler.getConfig().getOrderNum());
        bccLog.setExecuteResult(0);
        borrowCheckConfigLogMapper.insertSelective(bccLog);

        return nextHandler.handle(vo, serialNO);
    }
}
